#!/bin/bash

#ISO_TYPE='i686'
ISO_TYPE='dual'
MODE='wipe'
NETINSTALL=''
OFFLINE='-O'
INSTALL_TYPE=$OFFLINE # ($NETINSTALL or $OFFLIINE)
VERSION="`date +%Y.%m.%d-%H.%M`-alpha"


((`id -u`)) && echo "This script must be run with root privileges." && exit

while mount | grep $PWD/work/efiboot ; do umount $PWD/work/efiboot ; sleep 1 ; done ;
while mount | grep $PWD/work/; do umount $(mount | grep $PWD/work/) ; sleep 1 ; done ;

if   [ "$MODE" == 'wipe' ] # total wipe (preserving package cache)
then [ -d ./work/i686/root-image/isorepo   ] && mv ./work/i686/root-image/isorepo   ./isorepo-i686
     [ -d ./work/x86_64/root-image/isorepo ] && mv ./work/x86_64/root-image/isorepo ./isorepo-x86_64

     rm -rf ./work/ 2> /dev/null

     if   [ -d ./isorepo-i686 ]
     then mkdir -p ./work/i686/root-image
          mv ./isorepo-i686 ./work/i686/root-image/isorepo
     fi
     if   [ -d ./isorepo-x86_64 ]
     then mkdir -p ./work/x86_64/root-image
          mv ./isorepo-x86_64 ./work/x86_64/root-image/isorepo
     fi

elif [ "$MODE" == 'rebuild' ] # full rebuild
then rm -v ./work/build.make_*

elif [ "$MODE" == 'tweak' ] # tweaks only
then rm work/build.make_customize_root_image_$ISO_TYPE \
        work/build.make_prepare_$ISO_TYPE              \
        work/build.make_iso*                           2> /dev/null
fi

./build.sh -v $INSTALL_TYPE -T $ISO_TYPE -V $VERSION
